/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.resources.impl.internal;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

import nemos.resources.impl.IControl;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.Bundle;
import org.osgi.service.component.ComponentContext;

/**
 * UrlControl : The URL resources control service.
 * 
 * <p>
 * This class initializes {@link UrlResources} objects.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Service(IControl.class)
@Property(name = IControl.TYPE_PROPERTY, value = "java.net.URL")
@Component(name = "nemos.resources.url")
public class UrlControl implements IControl {

    /** The path property suffix */
    private static final String PATH_SUFFIX = ".path"; //$NON-NLS-1$

    /** The extension property suffix */
    private static final String EXTENSION_SUFFIX = ".extension"; //$NON-NLS-1$

    /** The resources bundle */
    private Bundle mBundle;
    /** The configured resources paths */
    private final Map<String, String> mResourcesPaths;
    /** The configured resources extensions */
    private final Map<String, String> mResourcesExtensions;

    /**
     * Constructor.
     */
    public UrlControl() {
        super();

        mResourcesPaths = new HashMap<String, String>();
        mResourcesExtensions = new HashMap<String, String>();
    }

    /**
     * Start the component.
     * 
     * @param pContext
     *            The bundle context
     */
    @Activate
    @Modified
    public void start(final ComponentContext pContext) {
        mBundle = pContext.getBundleContext().getBundle();

        final Enumeration<?> lEnum = pContext.getProperties().keys();
        while (lEnum.hasMoreElements()) {

            final String lKey = (String) lEnum.nextElement();
            if (lKey.endsWith(PATH_SUFFIX)) {
                final String lName = lKey.substring(0, lKey.length() - PATH_SUFFIX.length());
                mResourcesPaths.put(lName, (String) pContext.getProperties().get(lKey));
            }

            else if (lKey.endsWith(EXTENSION_SUFFIX)) {
                final String lName = lKey.substring(0, lKey.length() - EXTENSION_SUFFIX.length());
                mResourcesExtensions.put(lName, (String) pContext.getProperties().get(lKey));
            }
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.impl.IControl#getTimeToLive(java.lang.String,
     *      java.util.Locale)
     */
    @Override
    public long getTimeToLive(@SuppressWarnings("unused") final String pBaseName,
            @SuppressWarnings("unused") final Locale pLocale) {
        return Control.TTL_NO_EXPIRATION_CONTROL;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.impl.IControl#needsReload(java.lang.String,
     *      java.util.Locale, java.lang.String, java.lang.ClassLoader,
     *      java.util.ResourceBundle, long)
     */
    @SuppressWarnings("unused")
    @Override
    public boolean needsReload(final String pBaseName, final Locale pLocale, final String pFormat,
            final ClassLoader pLoader, final ResourceBundle pBundle, final long pLoadTime) {
        return false;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.impl.IControl#newBundle(java.lang.String,
     *      java.util.Locale, java.lang.String, java.lang.ClassLoader, boolean)
     */
    @Override
    public ResourceBundle newBundle(final String pBaseName, final Locale pLocale,
            @SuppressWarnings("unused") final String pFormat,
            @SuppressWarnings("unused") final ClassLoader pLoader,
            @SuppressWarnings("unused") final boolean pReload) {

        // Collect configuration
        String lPath = mResourcesPaths.get(pBaseName);
        if (lPath == null) {
            lPath = pBaseName;
        }
        String lExtension = mResourcesExtensions.get(pBaseName);
        if (lExtension == null) {
            lExtension = ""; //$NON-NLS-1$
        }

        // Build base name
        final String lBaseName = '/' + lPath + pLocale.toString();

        // Check at least one file is available
        final UrlResources lResources = new UrlResources(lBaseName + '/', lExtension, mBundle);
        if (lResources.getKeys().hasMoreElements()) {
            return lResources;
        }
        return null;
    }
}
